import React from 'react';
import './patientOriginManage.less';
import {browserHistory} from 'react-router';
import { Button, Table, Modal, Pagination, Form, message} from 'antd';
import $ from 'jquery';
import config from '../../../config/config';
const token = sessionStorage.getItem('token');
const allRouter=config.router;
class PatientOriginManage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            patientOriginList:[],//患者来源列表
        }
    }
    componentDidMount() {
        //获取患者来源列表
        this.getOriginPatientList();
    }
    //获取患者来源列表
    getOriginPatientList() {
        let url=allRouter.patientOrigin+'/source/list';
        //console.log(url);
        $.ajax({
            url:url,
            type: "GET",
            async: true,
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            success:data=>{
                if(data.code===200){
                    //给列表每一项增加索引
                    if (data.sources.length > 0) {
                        data.sources.forEach((ele, i) => {
                            ele.index= i + 1;
                        });
                    }
                    this.setState({
                        patientOriginList:data.sources
                    })
                }else{
                    console.log("获取患者来源列表出错");
                }
            },
            error:err=>{
                console.log(err);
            }
        });
    }
    //删除患者来源数据
    deleteOriginPatient(recode) {
        Modal.confirm({
            title: '删除患者来源',
            content: '操作提醒！禁用之后“患者来源”中将消失。',
            cancelText:'取消',
            className:'confirm-error',
            okText:'确定',
            onCancel: () => { message.info('取消删除该患者来源数据') },
            onOk: () => {
                //发送Ajax请求
                let url=allRouter.patientOrigin+'/source/delete';
                $.ajax({
                    url:url,
                    type: "POST",
                    async: true,
                    headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                    data:{
                        id:recode._id,
                    },
                    success:data=>{
                        if(data.code===200){
                            message.success('删除成功！', 2);
                            this.getOriginPatientList();
                        }else{
                            message.info('删除失败');
                        }
                    },
                    error:err=>{
                        console.log(err);
                    }
                });
            }
        })
    }
    //编辑患者来源状态
    editPatientOriginState(recode) {
        //真正修改状态
        let that=this;
        //实际更改状态
        let actualEditState=()=>{
            let url=allRouter.patientOrigin+'/source/update';
            $.ajax({
                url:url,
                type: "POST",
                async: true,
                headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
                data:{
                    id:recode._id,
                    state:currentState
                },
                success:data=>{
                    console.log(data);
                    if(data.code===200){
                        that.getOriginPatientList();
                    }else{
                        message.info('更改状态错误');
                    }
                },
                error:err=>{
                    console.log(err);
                }
            });
        };
        let currentState=recode.state;
        if(currentState==="启用"){
            currentState="禁用";
            Modal.confirm({
                title: '禁用患者来源',
                className:'confirm-error',
                content: '谨慎操作！“患者来源”选项中将消失！！',
                onCancel: () => { },
                onOk: () => {
                    actualEditState();
                }
            })
        }else if(currentState==="禁用"){
            currentState="启用";
            actualEditState();
        }

    }
    render() {
        const columns = [
            {
                title: '序号',
                dataIndex: 'index',
                width: 50,
            },
            {
                title: '来源名称',
                dataIndex: 'name',
                width: 250,
            },{
                title: '状态',
                dataIndex: 'state',
                width: 50,
                render:(text,recode)=> <Button onClick={this.editPatientOriginState.bind(this,recode)} type={text=='启用'?'primary':text=='禁用'?'danger':''}>{text=='启用'?'已启用，点击可禁用':text=='禁用'?'已禁用，点击可启用':''}</Button>
            }, {
                title: '操作',
                width: 150,
                render: (text, record) => (
                    <div>
                        <Button  type="primary" onClick={() => { browserHistory.push('addPatientOriginForm/'+record._id) }}>编辑</Button>
                        <Button onClick={this.deleteOriginPatient.bind(this, record)}  type="danger">删除</Button>
                    </div>
                )
            }
        ];
        return (
            <div className="container">
                <div className="store-manage-content">
                    {/*增加患者来源类型按钮*/}
                    <div className="storeAdd">
                        <Button onClick={() => { browserHistory.push('/addPatientOriginForm/0') }}  type="primary"> <img src={require('../../../../images/add.png')} alt=""/>新增患者来源类型</Button>
                    </div>
                    {/*患者来源类型列表 */}
                    <div className='all-roles-table'>
                        <Table
                            rowKey="index"
                            columns={columns}
                            dataSource={this.state.patientOriginList}
                            pagination={false}
                            bordered
                        />
                    </div>
                </div>
            </div>
        )
    }
}
export default PatientOriginManage;